
import java.io.*;


public class Splitter
{

	//Method to join the parts at the end
	public void join()
	{
		long off = 0;
        int readSize = PeerProcess.PieceSize;
        int numParts = PeerProcess.NumberOfPieces;
        int lastPartSize = PeerProcess.FileSize % PeerProcess.PieceSize;
        
        //Byte Array for each piece size
        byte[] data = new byte[PeerProcess.PieceSize];
        
        File filename = new File("peer_"+PeerProcess.ThisPeerId+'/'+PeerProcess.FileName);
        File filenamePart;
        
		try
		{
			RandomAccessFile outfile = new RandomAccessFile(filename,"rw");
			
			for(int i = 0; i < numParts; i++)
            {				
                if(i == numParts-1)
                	readSize = lastPartSize;
                
                try
                {
                	//Opening the pieces
                    filenamePart = new File("peer_"+PeerProcess.ThisPeerId+'/'+PeerProcess.FileName + i + ".sp");
    				RandomAccessFile infile = new RandomAccessFile(filenamePart,"r");
    				
                	//System.out.println("Joining part "+i);
                	
                	//Reading the pieces
                	infile.readFully(data, 0, readSize);
                	
                	//Writing the pieces one by one to outfile
                    outfile.seek(off);
                    outfile.write(data, 0, readSize);
                    
                    //Incrementing the file offset
                    off = off + (long)readSize;
                    
                    //Close the file and delete
    				infile.close();
    				filenamePart.delete();
                }
                catch (Exception e){
                    System.out.println(e);
		    e.printStackTrace();
                }
			}
			
			outfile.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	//Method to split the file into parts
	public void split()
	{
        long off = 0;
        int readSize = PeerProcess.PieceSize;
        int numParts = PeerProcess.NumberOfPieces;
        int lastPartSize = PeerProcess.FileSize % PeerProcess.PieceSize;
        
        //Byte Array for each piece size
        byte[] data = new byte[PeerProcess.PieceSize];
        
		try
		{
			//Use RandomAccessFile to read the file as an array of bytes
			File filename = new File("peer_"+PeerProcess.ThisPeerId+'/'+PeerProcess.FileName);
			RandomAccessFile infile = new RandomAccessFile(filename,"r");

			for(int i = 0; i < numParts; i++)
			{
				if(i == numParts-1) 
					readSize = lastPartSize;
				
				//Starting from the previously read piece
                infile.seek(off);
                
                try
                {
                	infile.readFully(data, 0, readSize);
                }
                catch (Exception e)
                {
                    System.out.println(e);
			e.printStackTrace();
                }
                
                //Writing the read piece to a new file
                File filePiece = new File("peer_"+PeerProcess.ThisPeerId+'/'+PeerProcess.FileName + Integer.toString(i) + ".sp");
				RandomAccessFile outfile = new RandomAccessFile(filePiece,"rw");
				
				outfile.write(data, 0, readSize);
				outfile.close();
				
				//Incrementing the file offset
                off = off + (long)readSize;

			}

 			filename.delete();         
            
        }
		catch(Exception e)
		{
           e.printStackTrace();
		}
	}
	
	//Opens the requested piece and then sends that as a byte array
	public byte[] returnPiece(int index)
	{
		byte[] data;
		int lastPartSize = 0;
		//Byte Array for each piece size
		if(index != PeerProcess.NumberOfPieces-1)
			data = new byte[PeerProcess.PieceSize];
		else
		{
			lastPartSize = PeerProcess.FileSize % PeerProcess.PieceSize;
			data = new byte[lastPartSize];
		}
		
        File filenamePart;
        
		try
		{
			//Opening the pieces
            filenamePart = new File("peer_"+PeerProcess.ThisPeerId+'/'+PeerProcess.FileName + index + ".sp");
    		RandomAccessFile infile = new RandomAccessFile(filenamePart,"r");
    				
            //System.out.println("Reading part "+index);
                	
            //Reading the pieces
            if(index != PeerProcess.NumberOfPieces-1)
            	infile.readFully(data, 0, PeerProcess.PieceSize);
            else
            	infile.readFully(data, 0, lastPartSize);
            
            infile.close();
            
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return data;
	}
	
	//Receives the piece and stores it.
	public void storePiece(int index, byte[] data)
	{
		File filename = new File("peer_"+PeerProcess.ThisPeerId+'/'+PeerProcess.FileName+Integer.toString(index)+".sp");
        
		try
		{
			//Create the piece .sp file and store it
			RandomAccessFile outfile = new RandomAccessFile(filename,"rw");
			
			outfile.write(data, 0, data.length);
                    
			outfile.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		
	}
}
